package com.dhcc.bpm.modules.system.wrapper;

import com.dhcc.bpm.common.cache.SysCache;
import com.dhcc.bpm.modules.system.entity.Menu;
import com.dhcc.bpm.common.cache.DictCache;
import com.dhcc.bpm.modules.system.vo.MenuVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName MenuWrapper
 * @Description 包装类,返回视图层所需的字段
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
public class MenuWrapper extends BaseEntityWrapper<Menu, MenuVO> {

	public static MenuWrapper build() {
		return new MenuWrapper();
	}

	@Override
	public MenuVO entityVO(Menu menu) {
		MenuVO menuVO = BeanUtil.copy(menu, MenuVO.class);
		assert menuVO != null;
		if (Func.isEquals(menu.getParentId(), BladeConstant.TOP_PARENT_ID)) {
			menuVO.setParentName(BladeConstant.TOP_PARENT_NAME);
		} else {
			Menu parent = SysCache.getMenu(menu.getParentId());
			if(parent != null){
				menuVO.setParentName(parent.getName());
			}
		}
		String category = DictCache.getValue("menu_category",String.valueOf( Func.toInt(menuVO.getCategory())));
		String action = DictCache.getValue("button_func", String.valueOf(Func.toInt(menuVO.getAction())));
		String open = DictCache.getValue("yes_no", String.valueOf(Func.toInt(menuVO.getIsOpen())));
		menuVO.setCategoryName(category);
		menuVO.setActionName(action);
		menuVO.setIsOpenName(open);
		return menuVO;
	}

	public List<MenuVO> listNodeVO(List<Menu> list) {
		List<MenuVO> collect = list.stream().map(menu -> BeanUtil.copy(menu, MenuVO.class)).collect(Collectors.toList());
		for (MenuVO i : collect){
			i.setNo(i.getId().toString());
			if(i.getParentId() != null){
				i.setParentNo(i.getParentId().toString());
			}
			i.setUrl(i.getPath() );
			i.setIcon(i.getSource() );
		}
		return ForestNodeMerger.merge(collect);
	}

}
